package com.qf.controller;

import com.aliyuncs.http.HttpRequest;
import com.qf.entity.User;
import com.qf.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Cookie;
import java.io.IOException;
import java.io.IOException;
import java.net.HttpCookie;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("*")
@CrossOrigin("*")
public class UserController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private HttpServletRequest request;


    Date date = new Date();

//    @RequestMapping("/login/{username}/{password}")
//    public Map selectLogin(@PathVariable("username")String username, @PathVariable("password")String password, HttpSession session){
//        User user = iUserService.selectLogin(username, password);
//        Map<String,Object>map = new HashMap<String,Object>();
//        if (user == null ){
//            map.put("code", "2");
//            map.put("msg", "登陆失败,用户名字或密码错误");
//            return map;
//        }
//        System.out.println(user.getuPassword());
//        System.out.println(password);
//        map.put("code", "1");
//        map.put("msg", "登陆成功");
//        map.put("userInfo", user);
//        session.setAttribute("user", user);
//        return map;
//    }

    @RequestMapping(value = "/adduser",method = RequestMethod.POST)
    public Map<String, String> addUser(@RequestBody User user) {

        User checkPhone = iUserService.CheckPhone(user.getuPhone());
        User checkName = iUserService.LoginUsername(user.getuName());
        Map<String, String> map = new HashMap<String, String>();

        if (checkPhone!=null){
            map.put("code","-1");
            map.put("msg","手机号码已激活");
            return map;
        }
        if (checkName!=null){
            map.put("code","-2");
            map.put("msg","用户名已经存在");
            return map;
        }

        user.setuStatus(1);
        user.setuRegisterDate(date);
        iUserService.addUser(user);
        map.put("code","1");
        map.put("msg", "注册成功");

        return map;
    }


    @RequestMapping("/loginUser/{username}/{password}")
    public Map LoginUsername(@PathVariable("username") String username, @PathVariable("password") String password,
                             HttpSession session,HttpServletResponse resp,HttpServletRequest req) {
        Map<String, Object> map = new HashMap<String, Object>();
        User user = iUserService.LoginUsername(username);

        if (user == null) {
            map.put("code", "3");
            map.put("msg", "用户名不存在或错误");
            return map;
        }
        if (!user.getuPassword().equals(password)) {
            map.put("code", "2");
            map.put("msg", "密码错误");
            return map;
        } else {
            map.put("code", "1");
            map.put("msg", "登陆成功");
            map.put("userInfo", user);
            session.setAttribute("user",user);
        }

        return map;
    }

        @RequestMapping("/checkPhone")
    public Map checkPhone(@RequestParam("phone")String uPhone){
        User checkPhone = iUserService.CheckPhone(uPhone);
        Map<String,Object> map = new HashMap<String, Object>();
        if (checkPhone!=null){
            map.put("code","-1");
            map.put("msg","手机号码已激活");
            return map;
        }else {
            map.put("code","1");
            map.put("msg","验证成功");
        }
        return map;
    }

    @RequestMapping("/checkUsername")
    public Map checkuName(@RequestParam("username")String username){
        User checkName = iUserService.LoginUsername(username);
        Map<String,Object> map = new HashMap<String, Object>();
        if (checkName!=null){
            map.put("code","-1");
            map.put("msg","用户名已经存在");
        }else {
            map.put("code","1");
            map.put("msg","用户名不存在");
        }
        return map;
    }

    @GetMapping("/loginJudge")
    public Map checkLogin(String method, String settle, HttpSession session, HttpServletResponse resp,HttpServletRequest req) throws IOException {
        Object obj = session.getAttribute("user");
        String userId="";


        Cookie[] cookies = req.getCookies();
        if (cookies!=null && cookies.length>0){
            for (int i=0;i<cookies.length;i++){
                if (cookies[i].getName().equals("userId")){
                    userId=cookies[i].getValue();
                }
            }
        }
        System.out.println(userId);
        String orderStr = settle;
        if (method.equals("login")) {//登录验证
            Map<String, Object> respMsg = new HashMap<String, Object>();

            if (userId == null) {
                respMsg.put("code", "1");
                respMsg.put("msg", "用户没有登陆");
                if (null != orderStr) {
                    resp.sendRedirect("login.html");
                }
                return respMsg;
            } else {
                respMsg.put("code", "2");
                return respMsg;
            }
        } else if (method.equals("quit")) {//退出登录

        }
        return null;

    }
}
